let token=localStorage.getItem("token")
let loginBox=document.querySelectorAll(".login-header")
let loginIfo=document.querySelector(".info")
let outInfo=document.querySelector(".out")
function autoLogin(){
  
  console.log(loginBox);
  console.log(token);
  // 
  
  if(token||Cookies.get("user") != null){
    loginBox[0].style.display="none"
    loginBox[1].style.display="block"
    let coo=Cookies.get("user")
    if(coo!=null){
      let str = JSON.parse(coo);
      console.log(str);
      console.log(str.username);
      let ustr=str.username
      loginIfo.innerHTML=`欢迎亲爱的 `+ustr
    }else{
      let ustr=localStorage.getItem("username");
      loginIfo.innerHTML=`欢迎亲爱的 `+ustr

    }
  }

}
autoLogin()


outInfo.onclick=function(){
  if(Cookies.get("user")!=null){
    let str = JSON.parse(Cookies.get("user"));
    Cookies.set("user", str, { expires: 0 }); 
 }
 if(localStorage.getItem("id")){
    localStorage.clear()
 }
  
  loginBox[1].style.display="none"
  loginBox[0].style.display="block"
}
// 
let nav=document.querySelectorAll(".navA a")
current=0;

nav.forEach(function(v,i){
    
  nav[i].classList.remove("nav-active");
  v.onclick=function(){
    nav[current].classList.remove("nav-active");
    nav[i].classList.add("nav-active");
    if(i==0){
      location.href="../index.html"
    }
    current=i;
}
})

console.log(location.search)
let obj=new URLSearchParams(location.search)
console.log(obj.get("id"));
let id=obj.get("id")
console.log(id);
let pagenum = 0;
let pagesize = 300;
let productArr = [];
let uid=localStorage.getItem("id")||"138133"
console.log(uid);
let leftBox=document.querySelector(".left")
let price=document.querySelector(".price")
let title=document.querySelector(".title")
let img=document.querySelector(".img")

// 商品详情
async function detail(id){
  console.log(id);
  let obj=await axios.get(detailAPI, { params: { pagenum,pagesize, uid:"138133"}})
  console.log(obj);
  productArr = [...obj.data.data];
  console.log(productArr);
  let pobj=productArr.find((v)=> v.pdesc==id)
  console.log(pobj);
  console.log(pobj.pimg);
  // 左侧图片
  let imgStr=`
  <div class="leftImg">
    <img src="${pobj.pimg}" class="img_pro_head" alt="${pobj.name}">
  </div>`
  leftBox.innerHTML=imgStr;
  let priceStr=`<i>￥</i><span class="now_price">${pobj.pprice}</span>`
  price.innerHTML=priceStr

  title.innerHTML=`${pobj.pname}`

  img.innerHTML=`<img src="${pobj.pimg}" alt="">`

  let addcar=document.querySelector(".buybtn")
  addcar.innerHTML=`<a class="gobuy open_spread">立即下单</a>

  <a class="addcart open_spread" onclick="addCar(${pobj.pid},this)"><span></span>加入购物车</a>`

  
}
detail(id)


let random=parseInt(Math.random()*10001)
let random2=parseInt(Math.random()*231)
let fan=parseInt(Math.random()*99601)
let fr=document.querySelector(".fr")
let mt5=document.querySelector(".mt5")
let count=document.querySelector(".count")
mt5.innerHTML=`<i class="this_stock">${random}</i>`
fr.innerHTML=`<p class="sales">交易成功：<i>${random2}</i></p>`
count.innerHTML=fan

let toTop=document.querySelector(".toTop")
toTop.onclick=function(){
  document.documentElement.scrollTo({top:0,behavior:"smooth"})
}

window.onscroll =  function () {
  console.log(document.documentElement.scrollTop);
  if (document.documentElement.scrollTop >= 99) {
    toTop.style.display="block";
    } else{
    toTop.style.display="none"
  }
}

// 利用事件委托和属性名
let number=document.querySelector(".number")
number.onclick = function (e) {
      var ev = e || event;
      var target = ev.target || ev.srcElement;

      // 数量+
      if (target.className === 'add') {
        setCount(target, 1);
      }

      // 数量-
      if (target.className === 'reduce') {
        setCount(target, -1)
      }
    }
    var oTr
    let  oCount= document.querySelector('.count-input');
       
    let oReduce 
    function setCount(target, step) {
      var oTr = target.parentNode.parentNode;
      oReduce =oTr.querySelector('.reduce');
      if (step < 0 && !oReduce.innerText) return;     
      // 数量到1后不可以再减
      var count = oCount.value * 1;

      count += step;
      oCount.value = count;
      if (step > 0) {   // 特殊处理加的部分
        oReduce.innerText = '-';
      } else {
        if (count < 1) {
          oReduce.innerText = '';
          oCount.value=`1`
          alert("数量已经达到最小")
        }
      }
    }
async function addCar(pid,btn){
  if(token||Cookies.get("user") != null){
    console.log(pid);
    let pnum= oCount.value
    console.log(pnum);
    let obj=await axios.get(addListAPI, { params: { uid, pid, pnum } })
    console.log(obj);
    alert("添加成功，请到购物车查看")
  }else{
    let res=window.confirm("请到登录页登录！");
    if(res){
      location.href="../html/login.html"
    }else{
      location.href="../index.html"
    }    
  } 

}

// 登录
let log=document.querySelector(".login")
log.onclick=function(){
  location.href="../html/login.html"
}
// 注册
let reg=document.querySelector(".reg")
reg.onclick=function(){
  location.href="../html/login.html"
}
// 跳转到自己
let self=document.querySelector(".headerImg")
self.onclick=function(){
  location.href="../index.html"
}
let car=document.querySelector(".car")
car.onclick= function(){
  if(token||Cookies.get("user") != null){
    location.href="../html/carList.html"
  }else{
      let res=window.confirm("请到登录页登录！");
      if(res){
        location.href="../html/login.html"
      }else{
        location.href="../index.html"
      }    
  }
  
}
